package com.apes.autoparts.supplier.platform;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.esb.processor.connector.http.HttpTransporter;
import com.apestech.framework.json.JSON;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 平台接口抽象类
 */
@Component
public abstract class SupplierPlatformApi<T, R> implements SupplierPlatformSearch<T, R> {


    protected String source;

    /**
     * 功能：业务单据类型
     */
    @Override
    public String getSource() {
        if (source == null) {
            throw new RuntimeException("平台类型不能为空！");
        }
        return source;
    }

    /***
     * 全量同步（指定供应商）
     */
    public abstract void synTotal(SupplierPlatformItem supplierPlatformItem);

    /***
     * 增量同步（指定供应商）
     */
    public abstract void synIncrement(SupplierPlatformItem supplierPlatformItem);

    /***
     * 获取单行商品（实时库存）
     */
    public abstract R getProductStock(SimpleRequest request);

    /***
     * 测试
     * @param supplierPlatformItem
     */
    public abstract void test(SupplierPlatformItem supplierPlatformItem);

    /***
     * 调用接口
     */
    public JSONObject requestCore(String url, Map content, Map<String,String> header) {
        HttpTransporter transporter = new HttpTransporter();
        transporter.setUrl(url);
//        transporter.setHeaders();
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        if (header != null) headers.putAll(header);
        transporter.setHeaders(headers);

        System.out.println(System.currentTimeMillis());
        try {
            System.out.println(JSON.toJSONString(new JSONObject(content)));

            SimpleRequest request = new SimpleRequest();
            request.setData(JSON.toJSONString(new JSONObject(content)));
            JSONObject result = JSONObject.parseObject((String) transporter.process(request));
            System.out.println(JSON.toJSONString(result));
            System.out.println(System.currentTimeMillis());
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    /***
     * 模拟长远接口
     */
    public abstract void simulationApi(SupplierPlatformItem supplierPlatformItem, JSONObject jo);

    // 获取原始数据
    public abstract List<Map> getRowData(SupplierPlatformItem supplierPlatformItem, String source);
}
